package com.liusu.spider.test;

import com.liusu.spider.js.File_;
import com.liusu.spider.js.JavaScript;
import com.liusu.spider.js.String_;
import com.liusu.spider.request.Request;
import com.liusu.spider.thread.Thread_;
import com.liusu.spider.util.RE;
import com.liusu.spider.wuzhou.Night;
import com.liusu.spider.wuzhou.Print;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Mimosa extends Print {

    public Request request = new Request();
    public JavaScript js = new JavaScript();

    public Night night = new Night();


    public HashMap<String,Object> links = new HashMap<>();

    /**
     * 国产
     */
    @Test
    public void contextLoads(){
        String url = "http://wuzhouqx.cn/video_conter/132373/3/index.html";
        /**
         * 播放页面代码
         */
        String video_page = request.get(url);
        String first_m3u8_url = RE.search(video_page,"<iframe src=\"(.+?)\"  frameborder=\"0\" scrolling=\"no\" width=\"100%\" height=\"100%\" ></iframe>");
        /**
         * https://videozm.whqhyg.com:8091/20210601/LYAF2ODp/index.m3u8
         */
        first_m3u8_url=first_m3u8_url.split("=")[1];
        print(first_m3u8_url);
        /**
         * 第一层m3u8的代码
         */
        String first_m3u8_content = request.get(first_m3u8_url);

        print(first_m3u8_content);
        /**
         * https://videozm.whqhyg.com:8091/20210601/LYAF2ODp/1000kb/hls/index.m3u8
         */
    }





    /**
     * 根据分区的第一页链接 获取分区视频的全部链接
     * @param url
     * @param domain
     */
    public void context1(String url,String domain){
        String first = request.get(url);
        Document document = Jsoup.parse(first);

        Elements pages = document.select("a.color-white");
        if(pages.size() > 1){
            /**
             * 最大的页数
             */
            Element max = pages.get(pages.size() - 3);
            int max1 = Integer.parseInt(max.text().trim());
            for (int i = 1; i <= max1; i++) {
                String[] split = url.split("/");
                split[split.length-2] = i+"";
                /**
                 * 每页的链接
                 */
                String real = Arrays.toString(split);
                /**
                 * 每页的代码
                 */
                String gb2312 = request.get(real, "gb2312");
                Document pagei = Jsoup.parse(gb2312);
                Elements a_list = pagei.select("ul.stui-vodlist li h4 a");
                for (Element a : a_list) {

                }
            }
        }

    }


    public void insert(Elements list,String domain){
        for (Element a : list) {
            /**
             * 完整的详情路径
             */
            String href = domain+a.attr("href").replace("/","");
            links.put(href,1);
        }
    }


    /**
     * 爬取弑神者小说
     */
    @Test
    public void novel(){
        String url = "http://www.shencou.com/read/0/883/index.html";
        /**
         * 目录页
         */
        String list = request.get(url,"gbk");
        JXDocument xpath = JXDocument.create(list);
        List<JXNode> list_a=xpath.selN("//div[@class='zjlist4']/ol/li/a");
        for (JXNode a : list_a) {

            /**
             * 具体每章的链接
             */
            String child_url = a.asElement().attr("href");

            String prefix = url.replace("index.html","");
            request.download(prefix+child_url,"D:\\video\\temp\\"+child_url);
            System.out.println(child_url);
            Thread_.sleep(1000);
        }
    }

    /**
     * 爬取恶魔高校
     */
    @Test
    public void imomoe(){
        String domain = "http://www.imomoe.la";
        /**
         * 列表页链接
         */
        String url = "http://www.imomoe.la/view/4975.html";
        /**
         * 列表页代码
         */
        String list_page = request.get(url, "GBK");
        Document document = Jsoup.parse(list_page);
        /**
         * 每集的链接集合
         */
        Elements alist = document.select("div#play_0 ul li a");
        int index = 0;
        for (Element a : alist) {
            String href = a.attr("href");
            /**
             * 完整的子页面链接
             */
            String real_child_url = domain + href;
            /**
             * iframe的src值
             */
            String iframe_src = js.get_iframe_src(0,index,real_child_url);
            print(iframe_src);
            /**
             * iframe页面代码
             */
            String iframe_page = request.get(iframe_src);

            Matcher matcher = Pattern.compile("var video =  '(\\S*)' ;").matcher(iframe_page);
            while (matcher.find()){
                String src = matcher.group(1);
                print(src);
                index++;
                request.download(src,"D:\\Application\\python\\Work\\Railgun\\school\\天降之物第一季-"+index+".mp4");
                print("第"+index+"文件下载完成");
            }

        }
    }

    /**
     *
     */
    @Test
    public void test2(){
        /*String s = "CNC|ShanDong_QingDao-124.129.154.70";
        String[] split = s.split("");
        System.out.println(Arrays.asList(split));*/
        String protect = request.get("http://127.0.0.1:8848/vue_elementui3/test/protect.html");
        boolean matches = protect.matches(".*?var video =  'http://ask.ivideo.sina.com.cn/v_play_ipad.php?vid=69805667&uid=1&pid=1&tid=334&plid=4001&prid=ja_7_2184731619&referrer=http%3A%2F%2Fvideo.sina.com.cn&ran=0.85629&r=video.sina.com.cn&v=4.1.43.10&&p=i&k=0375e85b07c72d48' ;.*?");
        System.out.println(matches);
    }


    /**
     * 爬取监狱学园
     */
    @Test
    public void prison(){
        String domain = "http://www.imomoe.la";
        /**
         * 列表页链接
         */
        String url = "http://www.imomoe.la/view/1398.html";
        /**
         * 列表页代码
         */
        String list_page = request.get(url, "GBK");
        Document document = Jsoup.parse(list_page);
        /**
         * 每集的链接集合
         */
        Elements alist = document.select("div#play_0 ul li a");
        int index = 0;
        for (Element a : alist) {
            String href = a.attr("href");
            /**
             * 完整的子页面链接
             */
            String real_child_url = domain + href;
            /**
             * 第一层m3u8的地址
             */
            String vid = js.get_vid(0,index,real_child_url);
            index++;
            print(vid);
            /**
             * 第一层m3u8的代码
             */
            HashMap<String,String> m = new HashMap<>();
            m.put("Origin","https://jianghu.live2008.com");
            String m3u8_1_string = request.get(vid,m);
            /**
             * 第二层m3u8的地址
             *
             */
            String m3u8_2_url_suffix = m3u8_1_string.split("\n")[2];
            /**
             * 江湖云域名
             */
            String domain_yun = this.get_prefix(vid);


            String m3u8_2_url = domain_yun+m3u8_2_url_suffix;
            /**
             * 第二层m3u8的代码
             *
             */
            String m3u8_2_string = request.get(m3u8_2_url,m);
            String[] split = m3u8_2_string.split("\n");

            HashMap<String,String> m3 = new HashMap<>();
            /**
             * 解密的KEY
             */
            String k = null;
            List<String> names = new ArrayList<>();
            int index1 = 0;
            for (String line : split) {

                line = line.trim();

                if(line.contains("DISCONTINUITY")){
                    break;
                }

                if(line.startsWith("#")){

                    if(line.contains("EXT-X-KEY")){
                        String k_url = domain_yun + this.search(line, "URI=\"(\\S*)\"");
                        k = request.get(k_url);
                    }
                    continue;
                }

                line = domain_yun+line;



                index1++;
                /**
                 * "D:\\Application\\python\\Work\\Railgun\\school\\"+file_name
                 */
                request.download(line,f("D:\\temporary\\{0}.ts",index1));


                print(f("第{0}文件已下载完成",index1));

            }
            for (int i = 1;i <= index1;i++) {

                File f = new File(f("D:\\temporary\\{0}.ts",i));

                // 需要解密的数组
                byte[] bytes = File_.readFileToByteArray(f);

                byte[] bytes_decrypt = night.AES_Decrypt(bytes, k.getBytes());
                print(f("{0}文件已解密",i));
                File_.writeByteArrayToFile(f("D:\\decrypt\\{0}.ts",i),bytes_decrypt);
                f.delete();
            }
            try {
                /**
                 * 合并后的文件
                 */
                File file = new File("D:\\formal\\监狱学园-"+index+".mp4");

                if (file.exists()) {
                    file.delete();
                }else{
                    file.createNewFile();
                }
                FileOutputStream stream = new FileOutputStream(file);

                for (int i = 1;i <= index1;i++) {

                    File f = new File(f("D:\\decrypt\\{0}.ts",i));


                    byte[] bytes = FileUtils.readFileToByteArray(f);

                    stream.write(bytes);

                    print(f("{0}文件已合并",i));
                    f.delete();
                }
                stream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * decrypt
     */
    @Test
    public void context1(){
        File f = new File("D:\\temporary");
        File[] files = f.listFiles();
        for (File file : files) {
            try {
                byte[] bytes = FileUtils.readFileToByteArray(file);
                byte[] bytes1 = night.AES_Decrypt(bytes,file.getName().split("_")[0].getBytes());
                FileUtils.writeByteArrayToFile(new File(f("D:\\decrypt\\{0}",file.getName())),bytes1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 手动合并
     */
    @Test
    public void context2(){

        try {
            File file = new File("D:\\formal\\test-"+1+".mp4");

            if (file.exists()) {
                file.delete();
            }else{
                file.createNewFile();
            }
            FileOutputStream stream = new FileOutputStream(file,true);

            for (int i = 1; i <= 493; i++) {



                File f = new File(f("D:\\decrypt\\{0}.ts",i));

                stream.write(FileUtils.readFileToByteArray(f));
                print(f+"已合并");
            }
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public String f(String content,Object ...list){
        if(list==null||list.length ==0 ){
            return content;
        }
        for (int i = 0; i < list.length; i++) {
            content = content.replace("{"+i+"}",list[i].toString());
        }
        return content;
    }
    public String get_prefix(String m3u8_url){
        String[] split = m3u8_url.split("/");
        String r = String.join("/",split[0],split[1],split[2]);
        return r;
    }

    /**
     * 用正则表达式提取一行中的特殊内容
     * @param content
     * @param pattern
     * @return
     */
    public String search(String content,String pattern){
        Pattern regular = Pattern.compile(pattern);
        Matcher matcher = regular.matcher(content);
        while (matcher.find()){
            String group = matcher.group(1);
            return group;
        }
        return null;
    }

    Map<String,String> name_link = new HashMap<>();

    /**
     * //fdfs.xmcdn.com/storages/4eae-audiofreehighqps/A7/F7/CMCoOSAD1XuiAAGPvgB7gz2q.m4a
     */
    @Test
    public void download_grey_series_single(){

        /**
         * 播放页面的链接
         */
        String real_child_url = "http://www.imomoe.la/player/1823-0-0.html";
        /**
         * 第一层m3u8的地址
         */
        String vid = js.get_vid(0,0,real_child_url);

        print("第一层m3u8的地址:"+vid);
        /**
         * 第一层m3u8的代码
         */
        String m3u8_1_string = request.get(vid);
        /**
         * 第二层m3u8的地址
         *
         */
        String m3u8_2_url_suffix = m3u8_1_string.split("\n")[2];
        /**
         * 域名
         */
        String domain = this.get_prefix(vid);

        String m3u8_2_url = domain+m3u8_2_url_suffix;
        print("第二层m3u8的地址:"+m3u8_2_url);
        /**
         * 第二层m3u8的代码
         *
         */
        String m3u8_2_string = request.get(m3u8_2_url);
        String[] split = m3u8_2_string.split("\n");

        HashMap<String,String> m3 = new HashMap<>();
        /**
         * 解密的KEY
         */
        String k = this.search(m3u8_2_string,"URI=\"(\\S*)\"");
        k = request.get(k);
        print("解密的KEY"+k);
        /**
         * 下载所有的ts片段
         */
        name_link.clear();
        List<String> names =this.download_ts(split);

        this.decrypt(names,k);

        this.merge_ts(names,"灰色的果实-1");


    }

    /**
     * 下载所有的ts片段
     * @param ts_url_list
     * @param domain_yun
     */
    public List<String> download_ts(String[] ts_url_list){
        List<String> names = new ArrayList<>();
        for (String line : ts_url_list) {

            line = line.trim();

            if(line.startsWith("#")){
                continue;
            }
            if(line.contains("https://ad")){
                continue;
            }

            print(line);
            String file_name = String_.rsplit(line, "/", -1,-1);
            request.download(line,f("temporary/{0}",file_name));
            names.add(file_name);
            name_link.put(file_name,line);
            print(f("{0}已下载完成",file_name));

        }
        return names;
    }

    /**
     *
     * 文件解密
     * @param names
     * @param k
     */
    public void decrypt(List<String> names,String k){
        for (String name : names) {

            File f = new File(f("temporary/{0}",name));
            if(!f.exists()){
                request.download(name_link.get(name),f("temporary/{0}",name));
            }
            // 需要解密的数组
            byte[] bytes = File_.readFileToByteArray(f);
            if(bytes.length%16 != 0){
                request.download(name_link.get(name),f("temporary/{0}",name));
            }
            byte[] bytes_decrypt = night.AES_Decrypt(bytes, k.getBytes());
            print(f("{0}已解密",name));
            File_.writeByteArrayToFile(f("decrypt/{0}",name),bytes_decrypt);
            f.delete();
        }
    }
    /**
     * javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
     *
     */
    public void merge_ts(List<String> names ,String formal){
        try {
            /**
             * 合并后的文件
             */
            File file = new File(f("formal/{0}.mp4",formal));

            if (file.exists()) {
                file.delete();
            }else{
                file.createNewFile();
            }
            FileOutputStream stream = new FileOutputStream(file);

            for (String name : names) {

                File f = new File(f("decrypt/{0}",name));


                byte[] bytes = FileUtils.readFileToByteArray(f);

                stream.write(bytes);

                print(f("{0}已合并",name));
                f.delete();
            }
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
